//
//  qyzm.m
//  PASystem
//
//  Created by michael on 16/8/24.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "qyzm.h"
#import "qiyezhaomuTableViewCell.h"
@implementation qyzm


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    UIColor *myColor = [UIColor colorWithWhite:0.8 alpha:0.3];
self.backgroundColor = myColor;

          }
    return self;
}
- (void)setId:(NSString *)id{

        self.currentPage = 1;

//NSLog(@"11%@",id);
_id = id;
 [self initview];
//[self request];
 
}
- (void)initview{
        self.ViewController.automaticallyAdjustsScrollViewInsets = YES;
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
 
_tableView.frame = CGRectMake(10,10, KScreenWidth-20, KScreenHeight-80-20);
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.dataSource = self;
_tableView.delegate = self;
//self.tableView.bounces = NO;
   

[self addSubview: _tableView];

     __weak typeof(self) weakSelf = self;

 [self.tableView addHeaderWithCallback:^{

        [weakSelf request]; //加载网络数据
    }];
     [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
       
             [weakSelf loadMore];
             
    }];



}
- (void)request{
 self.currentPage = 1;
NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':qyzmList,'service':DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,_id,(long)self.currentPage];
     NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
 _dataArray = [NSMutableArray array];
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
    
} [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
        if (self.dataArray.count ==0) {
        [self makeToast:@"暂时还没有数据哦"];
        }


    } failure:^(NSError *error) {
             [self.tableView headerEndRefreshing];

    }];



}
#pragma mark 加载更多
-(void)loadMore{
   NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':qyzmList,'service':DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,_id,(long)self.currentPage];
      NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

        self.currentPage++;
    
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
    
}

        [self.tableView footerEndRefreshing];
                [self.tableView reloadData];

    } failure:^(NSError *error) {
         [self.tableView footerEndRefreshing];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _dataArray.count;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *CellIdentifier = @"Cell";  

    qiyezhaomuTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell==nil) {  
        cell=[[qiyezhaomuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    NSDictionary *dic = _dataArray [indexPath.row];
    cell.dict = dic;

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
@end
